package cn.liangxxhu.gateway.mq.producer;

import cn.liangxxhu.gateway.mq.constant.MessageConstant;
import cn.liangxxhu.gateway.mq.domain.PayResultCallbackOrderEvent;
import cn.liangxxhu.rocketmq.core.param.MessageWrapper;
import cn.liangxxhu.rocketmq.core.AbstractCommonSendProduceTemplate;
import cn.liangxxhu.rocketmq.core.strategy.MessageSendStrategyFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OrderCancelSendProduce extends AbstractCommonSendProduceTemplate<PayResultCallbackOrderEvent> {


    public OrderCancelSendProduce(MessageSendStrategyFactory strategyFactory) {
        super(strategyFactory);
    }

    @Override
    protected MessageWrapper<PayResultCallbackOrderEvent> buildBaseSendExtendParam(PayResultCallbackOrderEvent messageSendEvent) {
        MessageWrapper<PayResultCallbackOrderEvent> messageWrapper = new MessageWrapper<>();
        // 事件名仅用于日志与可观测性
        messageWrapper.setEventName("延迟关闭订单");
        messageWrapper.setKeys(messageSendEvent.getOrderSn());
        messageWrapper.setTopic(MessageConstant.OrderMessageConstant.TOPIC);
        messageWrapper.setTag(MessageConstant.OrderMessageConstant.TAG);
        messageWrapper.setSentTimeout(2000L);
        // 5 秒后投递（由延迟策略解析具体级别/实现）
        messageWrapper.setDelayTime(5L);
        messageWrapper.setMessage(messageSendEvent);
        return messageWrapper;


    }

    @Override
    protected Message<?> buildMessage(MessageWrapper<PayResultCallbackOrderEvent> messageWrapper) {
        return MessageBuilder
                .withPayload(messageWrapper)
                .setHeader(MessageConst.PROPERTY_KEYS, messageWrapper.getKeys())
                .setHeader(MessageConst.PROPERTY_TAGS, messageWrapper.getTag())
                .build();
    }
}
